<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>课程管理系统</title>
    <script src="js/base.js"></script>

    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        button {
            padding: 8px 15px;
            margin: 5px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background-color: #0056b3;
        }
        input {
            padding: 8px;
            margin: 5px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
        }
        table {
            width: 100%;
            margin-top: 20px;
            border-collapse: collapse;
            text-align: center;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th {
            background-color: #007BFF;
            color: white;
            padding: 10px;
        }
        td {
            padding: 10px;
            background-color: #fff;
        }
        td a {
            color: #007BFF;
            text-decoration: none;
            margin: 0 5px;
        }
        td a:hover {
            text-decoration: underline;
        }
        #buttons {
            text-align: center;
            margin-top: 20px;
        }
        #buttons button {
            margin: 0 5px;
        }
    </style>

</head>
<body>
  <h1>课程管理</h1>
    1、<button onclick="goAddCourse();">新增课程</button><br/>
    2、<input id="searchKey" placeholder="请输入课程名"/><button onclick="onSearchClick();">查找课程</button><br/>
    <table id="courseData"border="1px">
        <tr>
            <td>课程号</td>
            <td>课程名称</td>
            <td>授课教师</td>
            <td>教室容量</td>
            <td>当前选课人数</td>
            <td colspan="3" align="center">操作</td>
        </tr>

    </table>
    <div id="buttons" style="position: relative;left: 200px;top:10px">

    </div>

<script>
    function goAddCourse(){
        window.location.href="addCourse.html";
    }



    let pageNo=1;
    let pageSize=5;
    let pageCount=0;
    let arr=null;
    function loadData(){
        let searchKey=document.getElementById("searchKey").value;

        let url="http://localhost:8080/teacher/page?pageNo="+pageNo+"&pageSize="+pageSize+"&searchKey="+searchKey;
        $.rest(url,"get",null,function (r){
            if (r.code==200){
                arr=r.data.data;
                pageCount=r.data.pageCount;
                let courseDataInnerHTML="<tr>\n" +
                    "            <td>课程号</td>\n" +
                    "            <td>课程名称</td>\n" +
                    "            <td>授课教师</td>\n" +
                    "            <td>教室容量</td>\n" +
                    "            <td>当前选课人数</td>\n" +
                    "            <td colspan=\"3\">操作</td>\n" +
                    "        </tr>";
                for(let i=0;i<arr.length;i++){
                    let item=arr[i];
                    courseDataInnerHTML=courseDataInnerHTML+"<tr>" +
                        "<td>"+item.id+"</td>" +
                        "<td>"+item.name+"</td>" +
                        "<td>"+item.tname+"</td>" +
                        "<td>"+item.maxLimit+"</td>" +
                        "<td>"+item.count+"</td>" +
                        "<td><a href='#' onclick='return onEditClick("+(i)+")'>修改</a></td>" +
                        "<td><a href='#' onclick='return onDelClick("+(i)+")'>删除</a></td>" +
                        "<td><a href='#' onclick='return onManagerClick("+(i)+")'>管理当前选课</a></td>" +
                        "</tr>";
                }
                document.getElementById("courseData").innerHTML= courseDataInnerHTML;

                let buttonsInnerHTML="";
                if (pageCount>0){
                    buttonsInnerHTML="<button onclick='onPrevClick();'>上一页</button>";
                    for(let i=0;i < pageCount;i++){
                        buttonsInnerHTML=buttonsInnerHTML+"<button onclick='onPageClick("+(i+1)+")'>"+(i+1)+"</button>";
                    }

                    buttonsInnerHTML=buttonsInnerHTML+"<button onclick='onNextClick();'>下一页</button>";
                }
                document.getElementById("buttons").innerHTML=buttonsInnerHTML;

            }
        });
    }
    loadData();
    function onPageClick(pNo){
        pageNo=pNo;
        loadData();

    }
    function onPrevClick(){
        if (pageNo==1){
            return;
        }
        pageNo--;
        loadData();
    }
    function onNextClick(){
        if (pageNo==pageCount){
            return
        }
        pageNo++;
        loadData();
    }
    function onSearchClick(){
        pageNo=1;
        loadData();
    }
    function onEditClick(index){
        let editCourse=arr[index];
        localStorage.setItem("editCourse_id",editCourse.id);
        localStorage.setItem("editCourse_name",editCourse.name);
        localStorage.setItem("editCourse_tname",editCourse.tname);
        localStorage.setItem("editCourse_maxLimit",editCourse.maxLimit);
        window.location.href="editCourse.html";
    }
    function onDelClick(index){
        let id=arr[index].id;
        $.rest("http://localhost:8080/teacher//delCourse?id="+id,"get",null,function (r){
            if (r.code==200){
                loadData();
            }
        });
    }
    function onManagerClick(index){
        let item=arr[index];
        localStorage.setItem("managerCourseId",item.id);
        localStorage.setItem("stuCount",item.count);
        localStorage.setItem("maxLimit",item.maxLimit);
        window.location.href="managerCourse.html";


    }
</script>
</body>
</html>